

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package com.itu.modernai;


import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardOpenOption.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Christoffer
 */
public class StatsWriter {
    //constants
    public static String WEAPONSTAT_PATH;
    public static String STATECHANGE_PATH;
   
    //Variables
    static File m_Weapons;
    static File m_States;
    
    private static StatsWriter instance = null;
    
    //Setup the writer.
    private StatsWriter() throws IOException
    {
        CreatePaths();
        SetupFiles();
    }
    //get instance
    public static StatsWriter getInstance() throws IOException
    {
        if(instance == null)
            instance = new StatsWriter();
        
        return instance;
    }
    
    void WriteStat(StatsType type, String message)
    {
        File fileName = null;
         if(type == StatsType.STATECHANGE)
        {
            fileName = m_States;
        }
        if(type == StatsType.WEAPON)
        {
            fileName = m_Weapons;
        }
        try {
            writeToFile(fileName, message);
        } catch (IOException ex) {
            Logger.getLogger(StatsWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    void WriteGene(String output, String fileName)            
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
        Date date = new Date();
        fileName += dateFormat.format(date)+".txt";
        File file = CreateFile(fileName);
         try {
            writeToFile(file, output);
        } catch (IOException ex) {
            Logger.getLogger(StatsWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void SetupFiles() throws IOException 
    {
        m_States= new File(STATECHANGE_PATH);
        m_Weapons = new File(WEAPONSTAT_PATH);
        
       if(!m_States.exists())
           m_States.createNewFile();
       if(!m_Weapons.exists())
           m_Weapons.createNewFile();
    }

    private void CreatePaths() 
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
        Date date = new Date();
        WEAPONSTAT_PATH = "WEAPONS_STATS_" + dateFormat.format(date)+".txt";
        STATECHANGE_PATH = "STATE_STATS_"+dateFormat.format(date)+".txt";
    }

    private void writeToFile(File fileName, String message) throws IOException 
    {
        if(fileName != null)
        {
             BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));
        
            bw.write(message);
            bw.newLine();

            bw.close();
        }
      
    }

    private File CreateFile(String fileName) 
    {
        File file = new File(fileName);
        try {                       
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(StatsWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return file;
    }
}
enum StatsType{
    WEAPON, STATECHANGE
}



